package com.dmz.workflow.listener;

import com.dmz.workflow.constant.LanguageExpressionConstants;
import com.dmz.workflow.constant.ProcessDefinitionConstants;
import com.dmz.workflow.util.ImcComponent;
import com.dmz.workflow.util.MessageContentWrapper;
import com.dmz.workflow.util.ObjectUtils;
import com.echronos.system.feign.IMemberFeign;
import com.echronos.system.resp.member.MemberDetailsResp;
import com.dmz.workflow.constant.MsgContentEnum;
import com.echronos.workflow.constant.ProcessConstants;
import com.dmz.workflow.entity.ProcessRelations;
import com.dmz.workflow.entity.StaffInfo;
import com.dmz.workflow.service.WorkflowNodeExtService;
import com.dmz.workflow.service.WorkflowNodeInfoService;
import com.dmz.workflow.service.WorkflowProcessRelationsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 更新抄送节点人员信息
 */
@Slf4j
@Service("copyService")
public class CopyToDelegate implements JavaDelegate {

    @Autowired
    private WorkflowNodeExtService workflowNodeExtService;

    @Autowired
    private WorkflowNodeInfoService workflowNodeInfoService;

    @Resource
    private ImcComponent imcComponent;

    @Autowired
    private MessageContentWrapper messageContentWrapper;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IMemberFeign memberFeign;

    @Autowired
    private WorkflowProcessRelationsService workflowProcessRelationsService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void execute(DelegateExecution execution) {
        String processInstanceId = execution.getProcessInstanceId();
        StaffInfo staffInfo = new StaffInfo();
        staffInfo.setProcessInstanceId(processInstanceId);
        staffInfo.setTypeNode(ProcessDefinitionConstants.DUPLICATE_NODE);
        staffInfo.setStatus(ProcessConstants.PERSON_APPROVED_STATUS);
        workflowNodeInfoService.updateNodeArriveStatus(processInstanceId, execution.getCurrentActivityId(), ProcessDefinitionConstants.DUPLICATE_NODE, null, ProcessConstants.NODE_ARRIVED_STATUS);
        workflowNodeExtService.updateNodeExtByProcessInstanceIdAndNodeId(staffInfo);
        List<StaffInfo> staffInfos = workflowNodeExtService.selectListByProcessInstanceIdAndNodeType(processInstanceId, ProcessDefinitionConstants.DUPLICATE_NODE);
        if (ObjectUtils.isNotEmpty(staffInfos)) {
            List<Integer> userIdList = new ArrayList<>();
            for (StaffInfo info : staffInfos) {
                if (ObjectUtils.isNotEmpty(info.getUserId())) {
                    userIdList.add(info.getUserId());
                }
            }
            if (ObjectUtils.isNotEmpty(userIdList)) {
                log.info("抄送人员列表：{}", userIdList);
                ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
                MemberDetailsResp memberDetailsResp = memberFeign.queryMemberById(Integer.valueOf(processInstance.getStartUserId()));
                imcComponent.imcAssistantSender(
                        messageContentWrapper.generateNotCommentMessageBody(MsgContentEnum.MESSAGE_AUTO_CC_CONTENT, processInstanceId),
                        memberDetailsResp.getUserId(),
                        userIdList, null);
                workflowProcessRelationsService.save(ProcessRelations.builder().processInstanceId(processInstance.getProcessInstanceId()).userIds(StringUtils.join(userIdList, LanguageExpressionConstants.LANGUAGE_COMMA)).build());
            }
        }
    }

}



